<template>
  <el-dialog v-model="dialogShow8" width="60%">
    <div class="titleShow">
      <div class="titles">{{ $t('licence.sys_protect') }}</div>
    </div>
    <div class="conten-main-t flexBetween">
      <div class="l">
        <span class="addstyle">{{ $t('licence.para_guojia') }}</span>
        <el-select v-model="countryCode" clearable @change="countryChanged"
          :placeholder="$t('licence.para_xuanze')">
          <el-option v-for="item in optionsCountry" :key="item.machineCode"
            :label="lang == 0 ? item.country : item.countryEn" :value="item.machineCode">
          </el-option>
        </el-select>
      </div>
      <div class="r">
        <span class="dialog-footer">
          <el-button @click="dialogShow8 = false">{{ $t('licence.cancel') }}</el-button>
          <el-button type="primary"
            @click="dialogClick8Check($t('licence.sys_set_verify'))">{{ $t('licence.determine') }}</el-button>
          <el-button type="info" @click="getProtectParamByFirmware">{{ $t('licence.loading') }}</el-button>
        </span>
      </div>
    </div>

    <div class="conten-main-b">
      <ul class="head">
        <li>{{ $t('licence.fau_num') }}</li>
        <li>{{ $t('licence.par_name') }}</li>
        <li>{{ $t('licence.par_resolvingPower') }}</li>
        <li>{{ $t('licence.par_protect') }}</li>
      </ul>
      <ul v-for="item in array" :key="item.id">
        <li>{{ item.id }}</li>
        <li>{{ item.name }}</li>
        <li>{{ item.radio }}</li>
        <li>
          <el-input v-model="item.parameter" :placeholder="item.parameter"
            class="inputW" @change="changeParameters(item)"></el-input>
        </li>
      </ul>
    </div>
  </el-dialog>
  <el-dialog v-model="dialogShow8Check" width="30%">
    <div class="titleShow">
      <div class="titles">{{ $t('licence.sys_set_verify') }}</div>
    </div>
    <el-form :inline="true" :model="formInline" ref="formInlines" class="demo-form-inline">
      <el-form-item :label="$t('licence.sys_set_password')" prop="num_8_0">
        <el-input style="width: 300px" v-model="formInline.num_8_0"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogShow8Check = false">{{ $t('licence.cancel') }}</el-button>
        <el-button type="primary" @click="instruction">{{ $t('licence.determine') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { commandControl } from "@/api/inveter/inveter";
import { getParameter, getAllCountry, getProtectParamByFirmwares } from "@/api/dianzhan/parameter.js"; //保护设置参数
import { useI18n } from 'vue-i18n'
import { asString } from "html2canvas/dist/types/css/types/color";
const { t } = useI18n()
const props = defineProps(['ids','lang','powerStation','userInfo']);
const dialogShow8 = ref(false) //保护设置
const dialogShow8Check = ref(false) //保护设置
const formInline = ref({
  num_8_0: '' as any,
})
const countryCode = ref()
const formInlines = ref<ElFormInstance>();
const requestCount =  ref(0)
//保护参数设置保存
let parameterData = ref({
  country: '',
  faultOneHight: '' as any,
  faultOneLow: '' as any,
  faulTwoHight: '' as any,
  faultTwoLow: '' as any,
  faultThreeHight: '' as any,
  faultThreeLow: '' as any,
  protectEnable: '' as any,
  protectOneSection: '' as any,
  protectOneSectionTime: '' as any,
  protectTwoSection: '' as any,
  protectTwoSectionTime: '' as any,
  recoveryValue: '' as any,
  overvoltage: '' as any,
  overcurrentThreshold: '' as any,
  instantThreshold: '' as any,
  componentThreshold: '' as any,
  oLThreshold: '' as any,
  vDThreshold: '' as any,
  uvOneSection: '' as any,
  uvOneSectionTime: '' as any,
  uvTwoSection: '' as any,
  uvTwoSectionTime: '' as any,
  uvValue: '' as any,
  boostThreshold: '' as any,
  invthreshold: '' as any,
  pvthreshold: '' as any,
  pvstopThreshold: '' as any,
  pvhightThreshold: '' as any,
  pvothreshold: '' as any,
  pqOneSection: '' as any,
  pqOneSectionTime: '' as any,
  pqRecoveryValue: '' as any,
  psVoltageKp: '' as any,
  psVoltageKi: '' as any,
  psCurrentKp: '' as any,
  psCurrentKi: '' as any,
  boostVoltageKp: '' as any,
  boostVoltageKi: '' as any,
  boostCurrentKp: '' as any,
  boostCurrentKi: '' as any,
  uqOneSection: '' as any,
  uqOneSectionTime: '' as any,
  uqRecoveryValue: '' as any,
  reserveOne: '' as any,
  reserveTwo: '' as any,
})
const optionsCountry = ref([
  {
    country: 0,
    countryEn: '',
    code: '',
    machineCode:''
  }
])
let array = ref([])
const assign = ()=>{
  array.value = [
      { id: 1, name: t('licence.num1'), radio: 1, parameter: parameterData.value.protectEnable,key:'protectEnable' },
      { id: 2, name: t('licence.num2'), radio: 0.1, parameter: parameterData.value.protectOneSection,key:'protectOneSection' },
      { id: 3, name: t('licence.num3'), radio: 0.1, parameter: parameterData.value.protectOneSectionTime,key:'protectOneSectionTime' },
      { id: 4, name: t('licence.num4'), radio: 0.1, parameter: parameterData.value.protectTwoSection,key:'protectTwoSection' },
      { id: 5, name: t('licence.num5'), radio: 20, parameter: parameterData.value.protectTwoSectionTime,key:'protectTwoSectionTime' },
      { id: 6, name: t('licence.num6'), radio: 0.1, parameter: parameterData.value.recoveryValue,key:'recoveryValue' },
      { id: 7, name: t('licence.num7'), radio: 0.1, parameter: parameterData.value.overvoltage,key:'overvoltage' },
      { id: 8, name: t('licence.num8'), radio: 0.01, parameter: parameterData.value.overcurrentThreshold,key:'overcurrentThreshold' },
      { id: 9, name: t('licence.num9'), radio: 0.01, parameter: parameterData.value.instantThreshold,key:'instantThreshold' },
      { id: 10, name: t('licence.num10'), radio: 0.1, parameter: parameterData.value.componentThreshold,key:'componentThreshold' },
      { id: 11, name: t('licence.num11'), radio: 0.1, parameter: parameterData.value.oLThreshold,key:'oLThreshold' },
      { id: 12, name: t('licence.num12'), radio: 0.1, parameter: parameterData.value.vDThreshold,key:'vDThreshold' },
      { id: 13, name: t('licence.num13'), radio: 0.1, parameter: parameterData.value.uvOneSection,key:'uvOneSection' },
      { id: 14, name: t('licence.num14'), radio: 20, parameter: parameterData.value.uvOneSectionTime,key:'uvOneSectionTime' },
      { id: 15, name: t('licence.num15'), radio: 0.1, parameter: parameterData.value.uvTwoSection,key:'uvTwoSection' },
      { id: 16, name: t('licence.num16'), radio: 20, parameter: parameterData.value.uvTwoSectionTime,key:'uvTwoSectionTime' },
      { id: 17, name: t('licence.num17'), radio: 0.1, parameter: parameterData.value.uvValue,key:'uvValue' },
      { id: 18, name: t('licence.num18'), radio: 0.01, parameter: parameterData.value.boostThreshold,key:'boostThreshold' },
      { id: 19, name: t('licence.num19'), radio: 0.01, parameter: parameterData.value.invthreshold,key:'invthreshold' },
      { id: 20, name: t('licence.num20'), radio: 0.1, parameter: parameterData.value.pvthreshold,key:'pvthreshold' },
      { id: 21, name: t('licence.num21'), radio: 0.1, parameter: parameterData.value.pvstopThreshold,key:'pvstopThreshold' },
      { id: 22, name: t('licence.num22'), radio: 0.1, parameter: parameterData.value.pvhightThreshold,key:'pvhightThreshold' },
      { id: 23, name: t('licence.num23'), radio: 0.01, parameter: parameterData.value.pvothreshold,key:'pvothreshold' },
      { id: 24, name: t('licence.num24'), radio: 0.01, parameter: parameterData.value.pqOneSection,key:'pqOneSection' },
      { id: 25, name: t('licence.num25'), radio: 20, parameter: parameterData.value.pqOneSectionTime,key:'pqOneSectionTime' },
      { id: 26, name: t('licence.num26'), radio: 0.01, parameter: parameterData.value.pqRecoveryValue,key:'pqRecoveryValue' },
      { id: 27, name: t('licence.num27'), radio: 1, parameter: parameterData.value.psVoltageKp,key:'psVoltageKp' },
      { id: 28, name: t('licence.num28'), radio: 1, parameter: parameterData.value.psVoltageKi,key:'psVoltageKi' },
      { id: 29, name: t('licence.num29'), radio: 1, parameter: parameterData.value.psCurrentKp,key:'psCurrentKp' },
      { id: 30, name: t('licence.num30'), radio: 1, parameter: parameterData.value.psCurrentKi,key:'psCurrentKi' },
      { id: 31, name: t('licence.num31'), radio: 1, parameter: parameterData.value.boostVoltageKp,key:'boostVoltageKp' },
      { id: 32, name: t('licence.num32'), radio: 1, parameter: parameterData.value.boostVoltageKi,key:'boostVoltageKi' },
      { id: 33, name: t('licence.num33'), radio: 1, parameter: parameterData.value.boostCurrentKp,key:'boostCurrentKp' },
      { id: 34, name: t('licence.num34'), radio: 1, parameter: parameterData.value.boostCurrentKi,key:'boostCurrentKi' },
      { id: 35, name: t('licence.num35'), radio: 0.01, parameter: parameterData.value.uqOneSection,key:'uqOneSection' },
      { id: 36, name: t('licence.num36'), radio: 20, parameter: parameterData.value.uqOneSectionTime,key:'uqOneSectionTime' },
      { id: 37, name: t('licence.num37'), radio: 0.01, parameter: parameterData.value.uqRecoveryValue,key:'uqRecoveryValue' },
      { id: 38, name: t('licence.num38'), radio: 1, parameter: parameterData.value.faultOneHight,key:'faultOneHight' },
      { id: 39, name: t('licence.num39'), radio: 1, parameter: parameterData.value.faultOneLow,key:'faultOneLow' },
      { id: 40, name: t('licence.num40'), radio: 1, parameter: parameterData.value.faulTwoHight,key:'faulTwoHight' },
      { id: 41, name: t('licence.num41'), radio: 1, parameter: parameterData.value.faultTwoLow,key:'faultTwoLow' },
      { id: 42, name: t('licence.num42'), radio: 1, parameter: parameterData.value.faultThreeHight,key:'faultThreeHight' },
      { id: 43, name: t('licence.num43'), radio: 1, parameter: parameterData.value.faultThreeLow,key:'faultThreeLow' },
      { id: 44, name: t('licence.num44'), radio: 1, parameter: parameterData.value.reserveOne,key:'reserveOne' },
      { id: 45, name: t('licence.num45'), radio: 1, parameter: parameterData.value.reserveTwo,key:'reserveTwo' }
  ]
}
// 切换国家获取参数
const countryChanged = async (value: any) => {
  let res = await getParameter({
    code: value,
  });
  parameterData.value = res.data;
  assign()
}
const prepareParam = () => {
  if (props.ids.length != 1) {
    return 0;
  } else {
    return props.ids[0];
  }
}
const getProtectParamByFirmware = async () => {
  let id = await prepareParam();
  if (id == 0) {
    ElMessage({
      type: "warning",
      message: t("licence.onlySingle"),
    });
    return
  }
  let data = {
    stationId: props.powerStation.id,
    commandId: 15,
    ids: id,
    lang: props.lang,
  };
  const loading = ElLoading.service({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      commandControl(data)
        .then(res => {
          setTimeout(() => {
            let res = getProtectParamByFirmwares({
              id: id,
            })
              .then(res => {
                parameterData.value = res.data;
                assign()
                countryCode.value = parameterData.value.country
                if(Number(res.data.country) == 0){
                  requestCount.value++
                  if(requestCount.value>2){
                    requestCount.value = 0
                    loading.close();
                    return ElMessage.error(t('licence.read_error'));
                  }
                  getProtectParamByFirmware()
                }
                else {
                  requestCount.value = 0
                  loading.close();
                }
              })
              .catch(err => {
                loading.close();
                return
              });
          }, 5000);
        })
        .catch(err => {
          return
        });
}
const dialogClick8Check = (name: any) => {
  formInline.value.num_8_0 = "";
  dialogShow8Check.value = true;
}
const changeParameters = (item)=>{
	console.log('item:', item.parameter)
  parameterData.value[item.key]=Number(item.parameter)
  console.log(parameterData.value,'parameterData.value[item.key]:', parameterData.value[item.key])
}
const instruction = async ()=>{
  if (formInline.value.num_8_0 != 123456) {
      ElMessage({
        type: "error",
        message: t("licence.pass_fail"),
      });
      return
    }
    if (parameterData.value.faultOneHight > 65536 ||
      parameterData.value.faultOneLow > 65536 ||
      parameterData.value.faulTwoHight > 65536 ||
      parameterData.value.faultTwoLow > 65536 ||
      parameterData.value.faultThreeHight > 65536 ||
      parameterData.value.faultThreeLow > 65536) {
      ElMessage({
        type: "error",
        message: t("licence.sys_shuzhi65536"),
      });
    }
    if (
      parameterData.value.protectEnable <= 32767 &&
      parameterData.value.protectOneSection <= 32767 &&
      parameterData.value.protectOneSectionTime <= 32767 &&
      parameterData.value.protectTwoSection <= 32767 &&
      parameterData.value.protectTwoSectionTime <= 32767 &&
      parameterData.value.recoveryValue <= 32767 &&
      parameterData.value.overvoltage <= 32767 &&
      parameterData.value.overcurrentThreshold <= 32767 &&
      parameterData.value.instantThreshold <= 32767 &&
      parameterData.value.componentThreshold <= 32767 &&
      parameterData.value.oLThreshold <= 32767 &&
      parameterData.value.vDThreshold <= 32767 &&
      parameterData.value.uvOneSection <= 32767 &&
      parameterData.value.uvOneSectionTime <= 32767 &&
      parameterData.value.uvTwoSection <= 32767 &&
      parameterData.value.uvTwoSectionTime <= 32767 &&
      parameterData.value.uvValue <= 32767 &&
      parameterData.value.boostThreshold <= 32767 &&
      parameterData.value.invthreshold <= 32767 &&
      parameterData.value.pvthreshold <= 32767 &&
      parameterData.value.pvstopThreshold <= 32767 &&
      parameterData.value.pvhightThreshold <= 32767 &&
      parameterData.value.pvothreshold <= 32767 &&
      parameterData.value.pqOneSection <= 32767 &&
      parameterData.value.pqOneSectionTime <= 32767 &&
      parameterData.value.pqRecoveryValue <= 32767 &&
      parameterData.value.psVoltageKp <= 32767 &&
      parameterData.value.psVoltageKi <= 32767 &&
      parameterData.value.psCurrentKp <= 32767 &&
      parameterData.value.psCurrentKi <= 32767 &&
      parameterData.value.boostVoltageKp <= 32767 &&
      parameterData.value.boostVoltageKi <= 32767 &&
      parameterData.value.boostCurrentKp <= 32767 &&
      parameterData.value.boostCurrentKi <= 32767 &&
      parameterData.value.uqOneSection <= 32767 &&
      parameterData.value.uqOneSectionTime <= 32767 &&
      parameterData.value.uqRecoveryValue <= 32767 &&
      parameterData.value.reserveOne <= 32767 &&
      parameterData.value.reserveTwo <= 32767
    ) {
      ElMessageBox.confirm(t("licence.Submit"), t("licence.Tips"), {
        confirmButtonText: t("licence.determine"),
        cancelButtonText: t("licence.cancel"),
        type: "warning",
      })
        .then(async () => {
          let data = {
            commandId: 8,
            ids: props.ids.join(","),
            parameters: parameterData.value,
            lang: props.lang,
          };
          await commandControl(data);
          ElMessage({
            type: "success",
            message: t("licence.success"),
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: t("licence.cancels"),
          });
        });
      dialogShow8.value = false;
      dialogShow8Check.value = false;
    } else {
      ElMessage({
        type: "error",
        message: t("licence.sys_shuzhi3"),
      });
    }
}
// 获取国家
const getAdd = async () => {
  let res = await getAllCountry();
  optionsCountry.value = res.data;
}

const openDialog = async () => {
  // 获取国家
  getAdd();
  dialogShow8.value = true;
  let res = await getParameter({
    code: props.userInfo.dept.countryCode,
  });
  countryCode.value = res.data.country
  parameterData.value = res.data;
  assign()
}
defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
.conten-main-t {
  margin-bottom: 20px;
}
// 弹框
.conten-main-b {
  margin-top: 20px;
  .head {
    background: #f5f8fa;
    height: 56px;
    line-height: 56px;
    // margin-bottom: 20px;

    li {
      font-size: 14px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 600;
      color: #515a6e;
    }
  }

  ul {
    padding: 0;
    display: flex;
    height: 56px;
    line-height: 56px;

    li {
      width: 20%;
      text-align: center;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #606266;

      &:nth-child(1) {
        width: 15%;
      }

      &:nth-child(2) {
        width: 50%;
      }

      &:nth-child(3) {
        width: 15%;
      }
    }
  }
}
.inputW {
  width: 93px;
  // background: #f5f7fb;
  border-radius: 4px;
}
</style>